<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>18.矩阵变换新</title>
	<style>
		canvas {
            background-color: rebeccapurple;
		}
	</style>
	<script src="../utils/index.js"></script>
	<link rel="stylesheet" href="../utils/ui.css">
</head>
<body>
	<div id="ui">
		<div class="x"></div>
		<div class="y"></div>
		<div class="scaleX"></div>
		<div class="scaleY"></div>
		<div class="rotate"></div>
	</div>
	<canvas id="gl" style="background-color: rebeccapurple;"></canvas>
	<script type="x-shader/x-vertex" id="vertex">
		uniform mat3 u_matrix;
		attribute vec2 a_position;
		void main(){
			vec3 position = ((u_matrix*vec3(a_position,1.)));
			gl_Position = vec4(position.xy,1,  1);
		}
	</script>
	
	<script type="x-shader/x-fragment" id="fragment">
		void main(){
			gl_FragColor =vec4(0.0,1.0,1.0,1.0);
		}
	</script>
	
	<script>
		 /** @type {HTMLCanvasElement} */   
		const canvas=document.getElementById("gl")
		const gl=canvas.getContext("webgl")
		canvas.width=1000
		canvas.height=1000
		//流程 1.创建着色器 编译 链接
		const vertex=document.getElementById("vertex").textContent
		const fragment=document.getElementById("fragment").textContent
		
		const vertexShader=gl.createShader(gl.VERTEX_SHADER)
		const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)
		gl.shaderSource(vertexShader,vertex)
		gl.shaderSource(fragmentShader,fragment)
		gl.compileShader(vertexShader)
		gl.compileShader(fragmentShader)
		const program=gl.createProgram()
		gl.attachShader(program,vertexShader)
		gl.attachShader(program,fragmentShader)
		gl.linkProgram(program)
		gl.useProgram(program)
		gl.viewport(0,0,1000,1000)


		const u_matrix=gl.getUniformLocation(program,"u_matrix")
		const translation=[500,500]
		let m3=webglUtils.Mat3
		let matrix=m3.translation(...translation)
		gl.uniformMatrix3fv(u_matrix,false,matrix)
		webglUtils.setupSlider(".x",{
			min:0,
			max:1000,
			value:500,
			step:1,
			handle:(e,v)=>{
				translation[0]=v.value
				draw()
			}
		})
		webglUtils.setupSlider(".y",{
			min:0,
			max:1000,
			value:500,
			step:1,
			handle:(e,v)=>{
				translation[1]=v.value
				draw()
			}
		})
		const scale=[1,1]
		webglUtils.setupSlider(".scaleX",{
			min:1,
			max:10,
			value:1,
			step:1,
			handle:(e,v)=>{
				scale[0]=v.value
				draw()
			}
		})
		webglUtils.setupSlider(".scaleY",{
			min:1,
			max:10,
			value:1,
			step:1,
			handle:(e,v)=>{
				scale[1]=v.value
				draw()
			}
		})
		let rotate=0
		webglUtils.setupSlider(".rotate",{
			min:0,
			max:360,
			value:0,
			step:1,
			handle:(e,v)=>{
				rotate=v.value
				draw()
			}
		})
		const a_position=gl.getAttribLocation(program,"a_position")
		const pos=[
			0, 0,
			30, 0,
			0, 150,
			0, 150,
			30, 0,
			30, 150,

			// top rung
			30, 0,
			100, 0,
			30, 30,
			30, 30,
			100, 0,
			100, 30,

			// middle rung
			30, 60,
			67, 60,
			30, 90,
			30, 90,
			67, 60,
			67, 90,
		]
		const centre=[-50,-75]//中心点

		const bufferData=gl.createBuffer()
			gl.bindBuffer(gl.ARRAY_BUFFER,bufferData)
			gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(pos), gl.STATIC_DRAW)
			gl.enableVertexAttribArray(a_position)
			gl.vertexAttribPointer(a_position, 2, gl.FLOAT, false, 0, 0)
		function draw(){
			matrix=m3.projection(1000,1000)//使用这个就不用在着色器内进行计算坐标了
			matrix=m3.translate(matrix,...translation)
			matrix=m3.scale(matrix,...scale)
			matrix=m3.rotate(matrix,webglUtils.degreesToRadians(rotate))
			matrix=m3.translate(matrix,...centre)

			gl.clear(gl.COLOR_BUFFER_BIT)
			gl.uniformMatrix3fv(u_matrix,false,matrix)
			
			gl.drawArrays(gl.TRIANGLES, 0, pos.length/2);
		}
		draw()

	</script>
</body>
</html>
